package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.model.AddOrderDTO;
import com.example.model.entity.Order;

@Mapper
public interface OrderMapper {
    @Insert("insert into tb_order(userId, status, startTime, address, worker) values(#{userId}, 'pending', #{startTime}, #{address}, #{worker})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addOrder(AddOrderDTO addOrderDTO);

    @Select("select id, userId, status, DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') as startTime, address, worker, commented, reback from tb_order where userId = #{userId} limit 1000")
    List<Order> getListOrderByUserId(@Param("userId") Integer userId);

    @Update("update tb_order set status = 'completed' where id = #{id}")
    int completeOrder(@Param("id") Integer id);

    @Select("select count(*) from tb_order where worker = #{worker} and status != 'completed'")
    int inProccessing(@Param("worker") Integer worker);

    @Select("select commented from tb_order where id = #{orderId}")
    boolean hasCommented(@Param("orderId") Integer orderId);

    @Update("update tb_order set commented = true where id = #{orderId}")
    int updateCommented(@Param("orderId") Integer orderId);

    @Select("select id, userId, status, DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') as startTime, address, worker, commented, reback from tb_order where userId = #{userId} and status = 'pending'")
    List<Order> getListOrderInPendingByUserId(@Param("userId") Integer userId);

    @Select("select id, userId, status, DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') as startTime, address, worker, commented from tb_order where userId = #{userId} and status = 'progress'")
    List<Order> getListOrderInProgressByUserId(@Param("userId") Integer userId);

    @Update("update tb_order set reback = true where id = #{id}")
    int updateReback(@Param("id") Integer id);

    @Select("select id, userId, status, DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') as startTime, address from tb_order where worker = #{worker} order by id desc limit 1000")
    List<Order> getListOrderByWorker(@Param("worker") Integer worker);

    @Update("update tb_order set status = 'progress' where id = #{id}")
    int progressOrder(@Param("id") Integer id);

    @Select("select id, userId, status, DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') as startTime, address from tb_order where worker = #{worker} and DATE(startTime) between #{startDate} and #{endDate} order by id desc limit 1000")
    List<Order> getListOrderByWorkerAndDate(@Param("worker") Integer worker, @Param("startDate") String startDate,
            @Param("endDate") String endDate);

    @Select("select worker from tb_order where id = #{id}")
    Integer getWorkerById(@Param("id") Integer id);
}
